package com.pcbly.web.action.crm;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.crm.JhdProductBean;
import com.pcbly.web.business.crm.JhdProductService;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.form.AnalyseCommonDataExt;

/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/

public class JhdProductAction extends BaseAction<JhdProductBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3744541381317811202L;

	protected JhdProductService jhdProductService; 
	
	public void setJhdProductService(JhdProductService jhdProductService) {
		this.jhdProductService = jhdProductService;
	}
	private JhdProductBean jhdProduct;
	
	private String orderBy;
	
	private Page<JhdProductBean> pages;
	
	private boolean asc = true;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public JhdProductBean getJhdProduct() {
		return jhdProduct;
	}
	public Page<JhdProductBean> getPages() {
		return pages;
	}

	public void setPages(Page<JhdProductBean> pages) {
		this.pages = pages;
	}
	public void setJhdProduct(JhdProductBean jhdProduct) {
		this.jhdProduct = jhdProduct;
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		jhdProduct=jhdProductService.getJhdProductBeanById(getId());
		extendCustomer = jhdProductService.findJhdProductBeanExtView(
				"t_crm_jhdproduct_ext", getId());
				
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		jhdProduct=jhdProductService.getJhdProductBeanById(getId());
		extendCustomer = jhdProductService.findJhdProductBeanExtEdit(
				"t_crm_jhdproduct_ext", getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}
	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();
		
		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<JhdProductBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = jhdProductService.findJhdProductBeanByCond(
				jhdProduct, pages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	public String add() throws Exception { 
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		jhdProduct=new JhdProductBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		extendCustomer = jhdProductService
				.findJhdProductBeanExtCreate("t_crm_jhdproduct_ext");
		return super.add();
	}
	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(jhdProduct.getId());
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_crm_jhdproduct_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);
		
		try{
		if (jhdProduct.getId() == null || "".equals(jhdProduct.getId().trim())) {// new 
			jhdProduct.setCreateDate(new Date());
			jhdProduct.setCreateUserId(getUserId());
			jhdProduct.setRecycled("0");
			jhdProduct=jhdProductService.saveJhdProductBean(jhdProduct);
			ext.setId(jhdProduct.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				jhdProductService.saveJhdProductBeanExt(sql);
			}
			setMyMessage(getText("entity.saved"));
		} else {// update
			jhdProductService.updateJhdProductBean(jhdProduct);
			
			boolean iflag = jhdProductService.getHasJhdProductBeanExtData(
					"t_crm_jhdproduct_ext", jhdProduct.getId());
			ext.setHaveData(iflag);
			ext.setId(jhdProduct.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				jhdProductService.saveJhdProductBeanExt(sql);
			}
			
			setMyMessage(getText("entity.updated"));
		}}catch(Exception e){
			e.printStackTrace();
		}
		extendCustomer = jhdProductService.findJhdProductBeanExtEdit(
				"t_crm_jhdproduct_ext", jhdProduct.getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}
	public String dwrSave(JhdProductBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new 
			jhdProductService.saveJhdProductBean(bean);
		} else {// update
			jhdProductService.updateJhdProductBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}
	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if(getId().indexOf(",")>=0){
			jhdProductService.removeJhdProductBeanByIds(getId());
		}else{
			jhdProductService.removeJhdProductBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		jhdProduct=null;
		return super.delete();
	}
	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if(id.indexOf(",")>=0){
			jhdProductService.removeJhdProductBeanByIds(id);
		}else{
			jhdProductService.removeJhdProductBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}
	
}